<template>
    <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible"
      :close-on-click-modal="false" draggable>
      <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
       <el-row :gutter="24">
            <el-col :span="24" class="mb20">
              <el-form-item label="专业等级" prop="zydjName">
                <el-input v-model="form.zydjName" placeholder="请输入专业等级"/>
              </el-form-item>
              </el-col>
           <el-col :span="24" class="mb20">
            <el-form-item label="专业名称" prop="zymcId">
                <el-select v-model="form.zymcId" filterable  style="width: 240px" :disabled="isProfessionalName">
                    <el-option v-for="item in options"  :key="item.zymcId" :label="item.zymcName" :value="item.zymcId"/>
                </el-select>
            </el-form-item>
           </el-col>
            <el-col :span="24" class="mb20">
              <el-form-item label="等级说明" prop="zydjDescription">
                <el-input v-model="form.zydjDescription" placeholder="请输入专业等级说明" type="textarea"/>
              </el-form-item>
              </el-col>
       </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<script setup lang="ts" name="JnglProfessionLevelDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { BasicTableProps, useTable } from "/@/hooks/table";
import { getObj, addObj, putObj, professionalnameList } from '/@/api/train/jnglProfessionLevel';
import { rule } from '/@/utils/validate';
import { ref } from 'vue'
import { getObjDetails } from '/@/api/admin/i18n';
const emit = defineEmits(['refresh']);
const options =reactive([{
	zymcName: '',
	zymcId: ''
}])

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 提交表单数据
const form = reactive({
      id:'',
	  zydjId: '',
	  zydjName: '',
	  zymcId: '',
	  zydjDescription: '',
	  remark: '',
	  zymcName: '',
});
// 专业名称判断变量
const isProfessionalName = ref(false)


// 定义校验规则
const dataRules = ref({
  zydjName: [{ required: true, message: '请输入专业等级', trigger: 'blur' },],
  zymcId: [{ required: true, message: '请选择专业名称', trigger: 'blur', },],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''
  isProfessionalName.value = false
	// const options = []
  // 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

  // 获取jnglProfessionLevel信息
  if (id) {
    form.id = id
    getjnglProfessionLevelData(id)
  }
    professionalnameList().then((res) => {
    options.length = 0;
    for (let index = 0; index < res.data.length; index++) {
        options.push({
					zymcName: res.data[index].zymcName,
					zymcId: res.data[index].zymcId,
        });
    }

    console.log("查询分类===>",options);
});


};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
    loading.value = true;

		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getjnglProfessionLevelData = (id: string) => {
  // 获取数据
  loading.value = true
  isProfessionalName.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>
